/*
 * @(#) $Header$
 *
 * Copyright (C)  2007  Daniel Léonard
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */
package ca.forklabs.baselib.util.function;

import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import ca.forklabs.baselib.util.UnaryFunction;

import java.lang.reflect.UndeclaredThrowableException;

/**
 * Class {@code UnaryClassFunction} is an {@link UnaryFunction} that calls back
 * a specific class method with one argument. It is the equivalent of C++
 * {@code std::ptr_fun} for unary functions.
 *
 * @param   <R>   the type of the returned object of the static method.
 * @param   <A>   the type of the argument object of the static method.

 * @author <a href="mailto:forklabs at gmail.com?subject=ca.forklabs.baselib.util.function.UnaryClassFunction">Daniel Léonard</a>
 * @version $Revision$
 */
public class UnaryClassFunction<R, A> implements UnaryFunction<R, A> {

//---------------------------
// Instance variables
//---------------------------

   /** The callback method. */
   private Method method;


//---------------------------
// Constructors
//---------------------------

   /**
    * Constructor.
    * @param   method   the static method.
    * @exception   NullPointerException   if the method is {@code null}.
    * @exception   IllegalArgumentException   if the method is not static.
    */
   public UnaryClassFunction(Method method) {
      this.setup(method);
      }

   /**
    * Constructor.
    * @param   clazz   the {@link Class} where the method is located.
    * @param   name   the name of the static method.
    * @param   param   the {@link Class} of the argument.
    * @exception   NullPointerException   if {@code clazz} is {@code null}.
    * @exception   NoSuchMethodException   if no method is found.
    * @exception   IllegalArgumentException   if the method is not static.
    */
   public UnaryClassFunction(Class<?> clazz, String name, Class<?> param) throws NoSuchMethodException {
      this.setup(clazz, name, param);
      }


//---------------------------
// Accessors and mutators
//---------------------------

   /**
    * Changes the method.
    * @param   method   the new method.
    * @exception   NullPointerException   if the method is {@code null}.
    * @exception   IllegalArgumentException   if the method is not static.
    */
   protected void setMethod(Method method) {
      if (null == method) {
         String message = this.getNullMethodErrorMessage();
         throw new NullPointerException(message);
         }

      int modifiers = method.getModifiers();
      boolean is_static = Modifier.isStatic(modifiers);
      if (false == is_static) {
         String message = this.getMethodNotStaticErrorMessage(method);
         throw new IllegalArgumentException(message);
         }

      this.method = method;
      }

   /**
    * Gets the method.
    * @return   the method.
    */
   protected Method getMethod() {
      return this.method;
      }


//---------------------------
// Instance methods
//---------------------------

   /**
    * Sets up this function.
    * @param   method   the static method.
    * @exception   NullPointerException   if the method is {@code null}.
    * @exception   IllegalArgumentException   if the method is not static.
    */
   protected void setup(Method method) {
      this.setMethod(method);
      }

   /**
    * Sets up this function.
    * @param   clazz   the {@link Class} where the method is located.
    * @param   name   the name of the static method.
    * @param   param   the {@link Class} of the argument.
    * @exception   NullPointerException   if {@code clazz} is {@code null}.
    * @exception   NoSuchMethodException   if no method is found.
    * @exception   IllegalArgumentException   if the method is not static.
    */
   @SuppressWarnings("hiding")
   protected void setup(Class<?> clazz, String name, Class<?> param) throws NoSuchMethodException {
      Method method = clazz.getDeclaredMethod(name, param);
      this.setup(method);
      }


//---------------------------
// Implemented method from ca.forklabs.baselib.util.UnaryFunction
//---------------------------

   /**
    * Invokes the static method with the parameter as its argument.
    * @param   argument   the parameter of the method.
    * @return   the result of the static method.
    * @exception   UndeclaredThrowableException   if an exception is thrown in
    *                                             the wrapped method.
    */
   @Override
   @SuppressWarnings({ "hiding", "unchecked" })
   public R invoke(A argument) {
      Method method = this.getMethod();
      R result;
      try {
         result = (R) method.invoke(null, argument);
         }
      catch (Exception e) {
         String message = e.getMessage();
         throw new UndeclaredThrowableException(e, message);
         }
      return result;
      }


//---------------------------
// Factory methods
//---------------------------

   /**
    * Creates a new unary class function.
    * @param   method   the static method.
    * @exception   NullPointerException   if the method is {@code null}.
    * @exception   IllegalArgumentException   if the method is not static.
    * @return   a new unary class function.
    */
   public static <R, A> UnaryClassFunction<R, A> create(Method method) {
      UnaryClassFunction<R, A> function = new UnaryClassFunction<>(method);
      return function;
      }

   /**
    * Creates a new unary class function.
    * @param   clazz   the {@link Class} where the method is located.
    * @param   name   the name of the static method.
    * @param   param   the {@link Class} of the argument.
    * @exception   NullPointerException   if {@code clazz} is {@code null}.
    * @exception   NoSuchMethodException   if no method is found.
    * @exception   IllegalArgumentException   if the method is not static.
    * @return   a new unary class function.
    */
   public static <R, A> UnaryClassFunction<R, A> create(Class<?> clazz, String name, Class<?> param) throws NoSuchMethodException {
      UnaryClassFunction<R, A> function = new UnaryClassFunction<>(clazz, name, param);
      return function;
      }


//---------------------------
// Error messages
//---------------------------

   /**
    * Gets the error message saying that the static method is {@code null}.
    * @return   the error message.
    */
   protected String getNullMethodErrorMessage() {
      String key = Resources.CLASS_FUNCTION_NULL_METHOD;
      String message = Resources.getLocalizedString(key);
      return message;
      }

   /**
    * Gets the error message saying that the static method is not static.
    * @param   method   the non-static method.
    * @return   the error message.
    */
   @SuppressWarnings("hiding")
   protected String getMethodNotStaticErrorMessage(Method method) {
      String key = Resources.CLASS_FUNCTION_NOT_STATIC;
      String message = Resources.getLocalizedString(key, method);
      return message;
      }

   }
